#include "pcmspec.h"

PCMSpec::PCMSpec():
    PCMSpec(0,0,AV_SAMPLE_FMT_NONE)
{


}

PCMSpec::PCMSpec(int sample, int channelLayout, AVSampleFormat format):
   _sampleRate(sample),_channelLayout(channelLayout),_format(format)
{



}

void PCMSpec::calFrameSize()
{

    _numChannel = av_get_channel_layout_nb_channels(_channelLayout);

    /**
        int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
        {
             return sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB ?
                0 : sample_fmt_info[sample_fmt].bits >> 3;
        }
     */
    _perChannelSize = av_get_bytes_per_sample(_format);
    _frameSize =_numChannel * _perChannelSize;

    _perSecondSize = _sampleRate * _frameSize;
}

int PCMSpec::getSampleRate() const
{
    // ffplay -ar 44800 -ac 2 -f f32le -i xxx.pcm
    return _sampleRate;
}

int PCMSpec::getChannelLayout() const
{

    return _channelLayout;
}

AVSampleFormat PCMSpec::getSampleFormat() const
{

    return _format;
}

int PCMSpec::getFrameSize()
{
    return _frameSize;
}

int PCMSpec::getNumChannel()
{
    return _numChannel;
}

int PCMSpec::getPerChannelSize()
{

    return _perChannelSize;
}

void PCMSpec::setSampleRate(int sampleRate)
{
    _sampleRate = sampleRate;
}

void PCMSpec::setChannelLayout(int channelLayout)
{
    _channelLayout = channelLayout;
}

void PCMSpec::setSampleFormat(AVSampleFormat format)
{
    _format = format;
}
